package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.qy.dao.DriveRecordDao;
import java.util.List;
import java.util.Map;
import java.sql.Types;
import com.qy.db.DbParameter;
import java.util.HashMap;
import java.util.ArrayList;
import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.DriveRecord;
import com.qy.db.BaseDao;

/**
* u_drive_Record daoImpl实现类的生成
* @author zhangwenlong
* @date Tue Jan 30 12:11:31 GMT+08:00 2018
*/ 
public class DriveRecordDaoImpl extends BaseDao  implements DriveRecordDao {

	@Override
	public boolean createDriveRecord(DriveRecord driveRecord) {
		boolean result = false;
		int id = -1;
		if (driveRecord.beginAdd()) {
			String sql = "insert into "+getTableName(driveRecord.getOrderDriverId())
				+" (id,order_driver_id,site,create_time) values (?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, driveRecord.getId()));
			param.put(2, new DbParameter(Types.INTEGER, driveRecord.getOrderDriverId()));
			param.put(3, new DbParameter(Types.VARCHAR, driveRecord.getSite()));
			param.put(4, new DbParameter(Types.TIMESTAMP, driveRecord.getCreateTime()));
			id = execLastId(sql, param);
			result = id > -1;
			driveRecord.commitAdd(result);
			driveRecord.setId(id);
		}
		return result;
	}

	@Override
	public List<DriveRecord> getDriveRecordList(int odId) {
		String sql = "select "+getTableName(odId)+" from u_drive_Record;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<DriveRecord> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<DriveRecord>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public DriveRecord getDriveRecordById(int id,int odId) {
		String sql = "select * from "+getTableName(odId)+" where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		DriveRecord driveRecord = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					driveRecord = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return driveRecord;
	}

	@Override
	public boolean updateDriveRecord(DriveRecord driveRecord) {
		boolean result = false;
		if (driveRecord.beginUpdate()) {
			String sql = "update "+getTableName(driveRecord.getOrderDriverId())
				+" set order_driver_id=?,site=?,create_time=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, driveRecord.getOrderDriverId()));
			param.put(2, new DbParameter(Types.VARCHAR, driveRecord.getSite()));
			param.put(3, new DbParameter(Types.TIMESTAMP, driveRecord.getCreateTime()));
			param.put(4, new DbParameter(Types.INTEGER, driveRecord.getId()));
			result = execNoneQuery(sql, param) > -1;
			driveRecord.commitUpdate(result);
		}
		return result;
	}

	public DriveRecord resultTo(ResultSet rs) throws SQLException {
		DriveRecord driveRecord = new DriveRecord();
		driveRecord.setId(rs.getInt("id"));
		driveRecord.setOrderDriverId(rs.getInt("order_driver_id"));
		driveRecord.setSite(rs.getString("site"));
		driveRecord.setCreateTime(rs.getTimestamp("create_time"));
		driveRecord.setOp(Option.None);
		return driveRecord;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}
	
	private String getTableName(int odId){
	    return "u_drive_Record"+(odId%10);
	}

}

